package cn.neusoft.service.impl;

import cn.neusoft.exception.AppException;
import cn.neusoft.mapper.RoleMenuMapper;
import cn.neusoft.po.Menu;
import cn.neusoft.mapper.MenuMapper;
import cn.neusoft.po.RoleMenu;
import cn.neusoft.query.MenuQuery;
import cn.neusoft.service.IMenuService;
import cn.neusoft.service.IRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hxj
 * @since 2023-12-19
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<Menu> getTree(MenuQuery query) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.hasText(query.getName()),"name",query.getName());

        List<Menu> all = baseMapper.selectList(queryWrapper);

        return tree(all);
    }

    /**
     * 得到一个菜单树的方法
     * @param all
     * @return
     */
    public List<Menu> tree(List<Menu> all){
        //构建了一个一级菜单的集合
        List<Menu> first = new ArrayList<>();

        Map<Long,Menu> map = new HashMap();
        //遍历所有菜单对象 找出一级菜单
        for (Menu menu:all){
            if (menu.getParentId()==null){
                map.put(menu.getId(),menu);//为了后面可以快速查找
                first.add(menu);//将一级菜单的对象加入一级菜单的集合
            }
        }
        //循环所有菜单  找出不是一级菜单的
        for (Menu menu:all){
            if (menu.getParentId()!=null){
                //先根据父菜单的id从map中取出父菜单对象
                Menu parent = map.get(menu.getParentId());
                //将自己放入父菜单的子菜单集合中
                parent.getMenuList().add(menu);
            }
        }

        return first;
    }

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> getRoleMenuTree(Integer roleId){
        //1、根据角色编号查询到该角色可以查看的菜单(二级菜单)
        //SELECT menu_id FROM t_role_menu WHERE role_id=14
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("menu_id");
        queryWrapper.eq("role_id",roleId);
        //list中就是查到的menu_id的集合
        List<Integer> list = roleMenuMapper.selectObjs(queryWrapper);

        //2、查询所有关联的父id
        queryWrapper = new QueryWrapper();
        queryWrapper.select("distinct parent_id")
                .in("id",list);

        List<Integer> parentIds = baseMapper.selectObjs(queryWrapper);

        //3、将二级菜单id和关联的一级菜单id拼装在一起
        list.addAll(parentIds);

        //通过上面查询得到的menu_id批量查询菜单对象
        /*
        SELECT * FROM t_menu WHERE id IN
        (34,56,78,88,23,.....)
        */
        List<Menu> menus = baseMapper.selectBatchIds(list);
        return tree(menus);
    }

    @Override
    public List<Menu> getRoleMenuList(Integer roleId){
        //SELECT menu_id FROM t_role_menu WHERE role_id=14
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("menu_id");
        queryWrapper.eq("role_id",roleId);
        //list中就是查到的menu_id的集合
        List<Integer> list = roleMenuMapper.selectObjs(queryWrapper);

        //通过上面查询得到的menu_id批量查询菜单对象
        /*
        SELECT * FROM t_menu WHERE id IN
        (34,56,78,88,23,.....)
        */
        List<Menu> menus = baseMapper.selectBatchIds(list);
        return menus;
    }

}
